package com.cgnpc.scp.manager.controller;

import cn.com.cgnpc.aep.bizcenter.appcenter.sdk.result.ApiResult;
import cn.com.cgnpc.aep.bizcenter.appcenter.sdk.utils.FileUtils;
import cn.com.cgnpc.aep.bizcenter.appcenter.sdk.utils.UrlUtil;
import com.cgnpc.cud.annotation.OperatorType;
import com.cgnpc.cud.annotation.UBA;
import com.cgnpc.cud.core.domain.AjaxResult;
import com.cgnpc.cuddemo.utils.IDHelper;
import com.cgnpc.framework.userinfo.CurrentUser;
import com.cgnpc.scp.common.bean.ScpApiResult;
import com.cgnpc.scp.common.bean.ScpFileInfoVO;
import com.cgnpc.scp.common.mybatis.entity.ScpCommonFileUpload;
import com.cgnpc.scp.common.pojo.UserInfo;
import com.cgnpc.scp.common.service.FileCenterService;
import com.cgnpc.scp.common.service.IScpCommonFileUploadService;
import com.cgnpc.scp.common.utils.*;
import com.cgnpc.scp.manager.service.FileCommonService;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.ResponseEntity;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.support.RequestContextUtils;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.*;

/**********************************
 * 用途说明: 公共接口-文件上传
 * 作者姓名: zhangyibing
 * 创建时间: 2020年10月16日11:37:00
 ***********************************/
@RestController
@RequestMapping("/scp/common")
public class FileCommonController {
    private static final Logger LOGGER = LoggerFactory.getLogger(DataDictGroupController.class);

    @Autowired
    FileCenterService fileCenterService;

    @Autowired
    CurrentUser currentUser;

    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private FileCommonService fileCommonService;

    @Autowired
    private IScpCommonFileUploadService scpCommonFileUploadService;

    @PostMapping("/uploadFile")
    //@UBA(module = "公共接口-文件上传", action = "公共接口-文件上传", channel = OperatorType.Button)
    @ApiOperation(value="公共接口-文件上传")
    public AjaxResult uploadFile(HttpServletRequest request,MultipartHttpServletRequest mRequest) {
        try {
            final String logInfo = "[run_file_uploadFile_UUID_" + UUID.randomUUID().toString() + "]";
            MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
            List<MultipartFile> files = multipartRequest.getFiles("file");
            //System.out.println("-----"+files.size());

            // begin 校验文件类型
            boolean isSafeFileWithUploadFile = FileUtil.isSafeFileWithUploadFile(files);

            if (!isSafeFileWithUploadFile) {
                return AjaxResultUtil.createErrorMessage(
                        "上传文件类型不匹配"
                );
            }
            // end 校验文件类型

            // 获取当前的登录用户信息
            UserInfo loginUserInfo = currentUser.getCurrentUserDetail();
            // 获取请求的语言reqLocale，cookie需要包含lang=cn，或者lang=en
            Locale reqLocale = RequestContextUtils.getLocaleResolver(request).resolveLocale(request);

            ScpApiResult<List<ScpFileInfoVO>> fileUploadResult = null;

                fileUploadResult = fileCenterService.uploadFileToFileCenterWithScpApiResult(logInfo, reqLocale,
                        loginUserInfo, files);
            System.out.println("fileUploadResult---"+ fileUploadResult);
            if("200".equals(fileUploadResult.getCode())){
                return AjaxResult.success().put("data", fileUploadResult);
            }else {
                return AjaxResult.error(fileUploadResult.getMsg()).put("data",fileUploadResult);
            }
        } catch (Exception e){
            LOGGER.error("uploadFile.error;", e);
            return AjaxResultUtil.createErrorMessage();
        }
    }

    @PostMapping("/getFileInfo")
    //@UBA(module = "公共接口-文件上传-查看详情", action = "公共接口-文件上传-查看详情", channel = OperatorType.Button)
    @ApiOperation(value="公共接口-文件上传-查看详情")
    public AjaxResult getFileInfo(@RequestBody Map<String, Object> params){
        try {
            return fileCommonService.getFileInfo(params.get("id") + "");
        } catch (Exception e) {
            LOGGER.error("getFileInfo.error;", e);
            return AjaxResultUtil.createErrorMessage();
        }
    }

    @PostMapping("/getFileList")
    //@UBA(module = "公共接口-文件上传-查看文件列表", action = "公共接口-文件上传-查看文件列表", channel = OperatorType.Button)
    @ApiOperation(value="公共接口-文件上传-查看文件列表")
    public AjaxResult getFileList(){
        try {
            return fileCommonService.getFileList();
        } catch  (Exception e) {
            LOGGER.error("getFileList.error;", e);
            return AjaxResultUtil.createErrorMessage();
        }
    }

}
